package com.audaque.springboot.foshanupload.mbmul.config.db1;

import com.audaque.springboot.foshanupload.mbmul.properties.MybatisProperties;

import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.mapping.DatabaseIdProvider;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zgb
 * @desc ...
 * @date 2022-12-03 20:34:08
 */
@Slf4j
@Configuration
@ConditionalOnProperty(value = "switch.mbmul",havingValue = "true",matchIfMissing = false)
public class Db1SqlSessionFactoryConfig {

    private static final String DATASOURCE_PREFIX = "db1";

    @Qualifier("db1DataSource")
    @Autowired
    private DataSource db1DataSource;


    @Autowired
    private DatabaseIdProvider databaseIdProvider;



    @Autowired
    private MybatisProperties mybatisProperties;




    /**
     * 自定义，默认已有
     * 通过自定义SqlSessionFactory保证多路径mapper.xml被扫描到
     * @return
     */
    @Bean
    public SqlSessionFactory db1SqlSessionFactory() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(db1DataSource);
        factoryBean.setDatabaseIdProvider(databaseIdProvider);
        String[] mapperLocations = mybatisProperties.getMapperLocations();
        List<String> mapperLocationList = new ArrayList<String>(Arrays.asList(mapperLocations));
        List<Resource[]> resourceArr = mapperLocationList.stream().map(e -> {
            try {
                return new PathMatchingResourcePatternResolver().getResources(e);
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
            return new Resource[0];
        }).collect(Collectors.toList());

        List<Resource> allResource = new ArrayList<>();
        for (Resource[] resources : resourceArr) {
            List<Resource> resourceList = new ArrayList<Resource>(Arrays.asList(resources));
            for (Resource resource : resourceList) {
                URI uri = resource.getURI();
                String path = uri.getPath();
                log.debug("path:"+path);
                if(path.contains(DATASOURCE_PREFIX)){
                    allResource.add(resource);
                }
            }

        }
        Resource[] arr=allResource.toArray(new Resource[allResource.size()]);
        factoryBean.setMapperLocations(arr);
        return factoryBean.getObject();
    }




}
